<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/27
 * Time: 下午 02:33
 */

namespace app\admin\controller;


use think\Request;

class Statics extends Common
{
    /**
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 后台统计-列表
     */
    public function index()
    {
        $start = $stop = $company_name = $start_time = $stop_time = $data['company_name'] = '';
        if(Request::instance()->isPost()){
            $start = input("post.start_time",'');
            if($start != ''){
                $start_time = $start." 00:00:00";
                $data['start_time'] = $start_time;
            }
            $stop = input("post.stop_time",'');
            if($stop != ''){
                $stop_time = $stop." 23:59:59";
                $data['stop_time'] = $stop_time;
            }
            $company_name = input("post.company_name",'');
            if($company_name != ''){
                $data['company_name'] = $company_name;
            }
        }

        //订单统计-数量
        $order_model = new \app\admin\model\Order();
        //未付款
        $zhuangtai = "create_time";
        $order_count = $order_model->getCountstatic($company_name, $zhuangtai, '0',$start_time,$stop_time);
        $data['wfk_order'] = $order_count;
        //待检车
        $zhuangtai = "pay_time";
        $order_count = $order_model->getCountstatic($company_name, $zhuangtai, '2',$start_time,$stop_time);
        $data['djc_order'] = $order_count;
        //已完成
        $zhuangtai = "finish_time";
        $order_count = $order_model->getCountstatic($company_name, $zhuangtai, '3',$start_time,$stop_time);
        $data['wc_order'] = $order_count;
        //已退款
        $zhuangtai = "update_time";
        $order_count = $order_model->getCountstatic($company_name, $zhuangtai, '',$start_time,$stop_time, 1);
        $data['tk_order'] = $order_count;
        //全部
        $data['order_count'] = $data['djc_order'] + $data['wc_order'];

        //订单统计-金额
        //未付款
        $zhuangtai = "create_time";
        $order_count = $order_model->getOrderJe($company_name, $zhuangtai, '0',$start_time,$stop_time);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['wfk_je'] = $order_count;
        //待检车
        $zhuangtai = "pay_time";
        $order_count = $order_model->getOrderJe($company_name, $zhuangtai, '2',$start_time,$stop_time);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['djc_je'] = $order_count;
        //已完成
        $zhuangtai = "finish_time";
        $order_count = $order_model->getOrderJe($company_name, $zhuangtai, '3',$start_time,$stop_time);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['wc_je'] = $order_count;
        //已退款
        $zhuangtai = "update_time";
        $order_count = $order_model->getOrderJe($company_name, $zhuangtai, '',$start_time,$stop_time, 1);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['tk_je'] = $order_count;
        //全部
        $data['order_je'] = $data['djc_je'] + $data['wc_je'];
        $data['order_je'] .= '.00';
        //核销统计-数量
        //已结算
        $zhuangtai = "distribut_time";
        $order_count = $order_model->gethx('1', $zhuangtai, $company_name,$start_time,$stop_time);
        $data['yjs_order'] = $order_count;
        //未结算
        $zhuangtai = "finish_time";
        $order_count = $order_model->gethx('0', $zhuangtai, $company_name,$start_time,$stop_time);
        $data['wjs_order'] = $order_count;

        //核销统计-金额
        //已结算
        $zhuangtai = "distribut_time";
        $order_count = $order_model->getje('1', $zhuangtai, $company_name,$start_time,$stop_time);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['yjs_je'] = $order_count;
        //未结算
        $zhuangtai = "finish_time";
        $order_count = $order_model->getje('0', $zhuangtai, $company_name,$start_time,$stop_time);
        $exit = strpos($order_count,'.');
        if($exit === false){
            if($order_count == 0){
                $order_count = "0.00";
            }else{
                $order_count = $order_count.".00";
            }
        }
        $data['wjs_je'] = $order_count;

        //检车站信息
        $check_station = db("company")->field("id,company_name")->where("is_delete",0)->select();
        $this->assign("check_station",$check_station);
        $data['stop_time'] = $stop;
        $data['start_time'] = $start;
        return view('',$data);
    }
}